package com.sx.interceptor;

import com.aliyuncs.utils.StringUtils;
import com.sx.basic.HttpConst;
import com.sx.basic.ResponseEntity;
import com.sx.basic.exception.BasicException;
import com.sx.service.system.IAdminService;
import org.apache.commons.io.IOUtils;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author AdinZ
 * @date 2020/11/13 15:08
 */
public class TokenHandler implements HandlerInterceptor {
    private final IAdminService adminService;

    public TokenHandler(IAdminService adminService) {
        this.adminService=adminService;
    }

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String token=request.getHeader(HttpConst.TOKEN);
        if(StringUtils.isEmpty(token))
            throw new BasicException(ResponseEntity.CODE_TOKEN_FAIL,"token错误");
        response.addHeader("Author","By.AdinZ");
        return this.adminService.isAuthority(token,request.getMethod(),request.getRequestURI());
    }
}
